package com.wisnews.service.sec.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wisnews.mapper.sec.OperatorMapper;
import com.wisnews.pojo.Input;
import com.wisnews.pojo.sec.entity.Operator;
import com.wisnews.util.DateUtil;
import com.wisnews.util.ShiroUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuyd
 * @since 2020-12-06
 */
@Service
@Slf4j
public class OperatorServiceImpl extends ServiceImpl<OperatorMapper, Operator> implements IService<Operator> {

    @Autowired
    private OperatorMapper operatorMapper;

    @Override
    public boolean save(Operator entity) {
        //生成盐
        String salt = ShiroUtil.createSalt();
        //保存盐
        entity.setSalt(salt);
        //密码加密
        entity.setPassword(ShiroUtil.encryption(entity.getPassword(),salt));

        entity.setCreateTime(DateUtil.getLocalDateTime());

        entity.setState("1");

        entity.setRecentPassword(ShiroUtil.encryption("1qaz123!QAZ",salt));

        boolean save = super.save(entity);

        return save;
    }



    public IPage<Operator> getOperatorInfo4Page(Input<Operator> operatorInput) {
        IPage<Operator> iPage = operatorInput.toPage();
        Operator operator = operatorInput.getReq();

        LambdaQueryWrapper<Operator> lambdaQueryWrapper = Wrappers.lambdaQuery();
        //条件查询
        lambdaQueryWrapper.like(StringUtils.isNotBlank(operator.getStaffId()),Operator::getStaffId,operator.getStaffId())
                .like(StringUtils.isNotBlank(operator.getCode()),Operator::getCode,operator.getCode())
                .like(StringUtils.isNotBlank(operator.getEmail()),Operator::getEmail,operator.getEmail())
                .like(StringUtils.isNotBlank(operator.getPhoneNumber()),Operator::getPhoneNumber,operator.getPhoneNumber())
                .like(StringUtils.isNotBlank(operator.getOpetatorName()),Operator::getOpetatorName,operator.getOpetatorName())
                .eq(StringUtils.isNotBlank(operator.getState()),Operator::getState,operator.getState());

        IPage<Operator> page = operatorMapper.getOperatorInfo4Page(iPage, lambdaQueryWrapper);
        return page;

    }


    @Override
    public Operator getOne(Wrapper<Operator> queryWrapper) {

        Operator one = super.getOne(queryWrapper);
        return one;
    }


}
